<?php

namespace Lambq\Mqtt\Console;

use Illuminate\Console\Command;

class createTaskCommand extends Command
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $signature = 'mqtt:task {name}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a new task class';

    /**
     * Install directory.
     *
     * @var string
     */
    protected $directory = '';

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle()
    {
        $name = $this->argument('name');

        if ($name)
        {
            $this->createTasks($name);

            $this->info('Done.');
        } else {
            $this->error('Please fill in the name!');
        }
    }

    protected function getStub($name)
    {
        return $this->laravel['files']->get(__DIR__."/stubs/$name.stub");
    }

    public function createTasks($name)
    {
        $homeController = $this->directory.'/Tasks/{$name}.php';
        $contents = $this->getStub('AllTask');

        $this->laravel['files']->put(
            $homeController,
            str_replace(
                ['DummyNamespace', 'DummyClass'],
                ['App\\Mqtt\\Tasks', $name],
                $contents
            )
        );
        $this->line('<info>task file was created:</info> '.str_replace(base_path(), '', $homeController));
    }
}